import { ConfigEnv, loadEnv, UserConfig } from 'vite'
import { resolve } from 'path'
import { wrapperEnv } from './build/utils'
import { createVitePlugins } from './build/plugin'

export default ({ command, mode }: ConfigEnv): UserConfig => {
	const root = process.cwd()
	const env = loadEnv(mode, root)
	const viteEnv = wrapperEnv(env)
	const { VITE_PORT, VITE_PUBLIC_PATH, VITE_DROP_CONSOLE } = viteEnv
	const isBuild = command === 'build'
	return {
		base: VITE_PUBLIC_PATH, //根目录
		build: {
			outDir: 'pcaktool', //输出目录
			target: 'es2015', //输出编码
			terserOptions: {
				compress: {
					keep_infinity: true,
					drop_console: VITE_DROP_CONSOLE, //删除打印信息
				},
			},
			// Turning off brotliSize display can slightly reduce packaging time
			brotliSize: false,
			chunkSizeWarningLimit: 2000,
		},
		//代理
		server: {
			host: '0.0.0.0',
			port: VITE_PORT, //端口
			proxy: {},
		},
		css: {
			preprocessorOptions: {
				less: {},
			},
		},
		resolve: {
			alias: {
				'@': resolve(__dirname, 'src'),
				'#': resolve(__dirname, 'types'),
			},
		},
		plugins: createVitePlugins(viteEnv, isBuild), //插件
	}
}
